@extends('admin.layouts.app')
@section('css')
        <!-- Custombox css -->
       
        <link href="{{asset('admin/plugins/bootstrap-datetimepicker/css/bootstrap-datetimepicker.min.css')}}" rel="stylesheet">
         
@endsection 

@section('content')

                        <div class="row">
                            <div class="col-sm-12">
                                <h4 class="page-title">工单管理</h4>
                                <ol class="breadcrumb">
                                    <li><a href="{{url('zadmin')}}">系统</a></li>
                                    <li><a href="{{url('zadmin/task')}}">工单管理</a></li>
                                    <li class="active">修改工单</li>
                                </ol>
                            </div>
                        </div>
                        
                        <div class="row">
                        	<div class="col-lg-12">
                        		<div class="card-box">
                                    <div class="row">
                            <h2 class="m-t-0 header-title">工单信息</h2>
                            <h2 class="m-t-0 header-title pull-right btn btn-danger" >工单总额：{{$data->total_money}}</h2>
                        </div>
                            <hr/>
                             @if(session('rs') || $errors->any())
                              <div class="alert alert-{{session('rs')['status']}}">
                        @if($errors->any())
                            @foreach($errors->messages() as $msg )
                                {{$msg[0]}}
                            @endforeach
                        @endif
                                {{ session('rs')['msg'] }}
                              </div>
                              @endif
                        			<div class="row">

			                        	<div class="col-md-12">
                                  
                           
                            <div class="row">
                                 <form method="post" action="{{url('zadmin/task/update',$data->id)}}" enctype="multipart/form-data">

                                     {{ csrf_field()}}
                                <div class="col-lg-12">
                                   
                                <div class="panel panel-border panel-default">
                                    <div class="panel-heading">
                                        <h3 class="panel-title">基本信息</h3>
                                    </div>
                                    <div class="panel-body">
                                        <div class="row">
                                            <div class="col-md-3">
                                                <div class="form-group ">
                                                <label class="col-md-4">客户<span class="text-danger">*</span></label>
            <div class="col-md-8">                                    
            <input class="form-control" name="client_name" value="{{$data->client_name}}" required=""/>
        </div>

                                                </div> 
                                            </div>
                                            <div class="col-md-3">
                                                <div class="form-group ">
                                                <label class="col-md-4">车辆品牌<span class="text-danger">*</span></label>
                                                 <div class="col-md-8">  
                    <input class="form-control" name="car_brand" value="{{$data->car_brand}}" required=""/>
                </div>

                                                </div>
                                            </div>

                                            <div class="col-md-3">
                                                <div class="form-group ">
                                                <label class="col-md-4">车牌号<span class="text-danger">*</span></label>
                                                 <div class="col-md-8">  
                    <input class="form-control" name="car_num" required="" value="{{$data->car_num}}" />
                </div>

                                                </div>
                                            </div>

                                            <div class="col-md-3">
                                                <div class="form-group ">
                                                <label class="col-md-4">付款方式<span class="text-danger">*</span></label>
                    <div class="col-md-8">                            
                    <select class="form-control" name="balance_type" required="">
                       
                        <option value="现金" {{$data->banlance_type=='现金'?'selected':''}}>现金</option>
                        <option value="月结" {{$data->banlance_type=='月结'?'selected':''}}>月结</option>
                        <option value="保险理赔" {{$data->banlance_type=='保险理赔'?'selected':''}}>保险理赔</option>
                                                    
                    </select>
                </div>

                                                </div>
                                            </div>

                                            
                                        </div>
                                   <div class="row m-t-5">
                                        <div class="col-md-4 ">
    <div class="form-group ">
    <label class="col-md-4">送修人<span class="text-danger">*</span></label>
    <div class="col-md-8">
     <input class="form-control" name="name" value="{{$data->name}}" required=""/>
 </div>
     </div>
</div>

<div class="col-md-4">
    <div class="form-group ">
    <label class="col-md-4">送修电话<span class="text-danger">*</span></label>
    <div class="col-md-8">
     <input class="form-control" name="phone" value="{{$data->phone}}" required=""/>
 </div>
     </div>
</div>
</div>
                                        <div class="row m-t-5">
                                            <div class="form-group col-md-4">
                                                <label class="col-md-4">送修日期<span class="text-danger">*</span></label>
                                                <div class="col-md-8"> 
                                                <input class="form-control date" name="arrived_at" value="{{$data->arrived_at}}"  /> 
                                                </div>
                                            </div>
                                            <div class="form-group col-md-4">
                                                <label class="col-md-4">送修里程<span class="text-danger">*</span></label>
                    <div class="input-group col-md-8">
                        <input name="miles" class="form-control" value="{{$data->miles}}" />
                        <span class="input-group-btn">
                        <button type="button" class="btn waves-effect waves-light btn-primary">公里</button>
                        </span>
                    </div> 
                                            </div>
                                           
                                            <div class="form-group col-md-4">
                                                <label class="col-md-4">维修类型<span class="text-danger">*</span></label>
                                                <div class="col-md-8">
                                                <select class="form-control " name="maintain_id" >
                                                @foreach($maintainTypes as $mt)
                                                    <option value="{{$mt->id}}" {{$mt->id==$data->maintain_id?'selected':''}}>{{$mt->name}}</option>
                                                @endforeach
                                                </select> 
                                            </div>
                                            </div>

                                            <div class="form-group col-md-4">
                                                <label class="col-md-4">完工日期<span class="text-danger">*</span></label>
                                                <div class="col-md-8"> 
                                                <input class="form-control date" name="completed_at" value="{{$data->completed_at}}"  /> 
                                                </div>
                                            </div>
                                            <div class="form-group col-md-8">
                                                <label class="col-md-2">折扣率</label>
                                                <div class="col-md-10">
                                                <input class="form-control " name="rate" value="{{$data->rate}}" /> 
                                            </div>
                                            </div>
                                            <div class="form-group col-md-8">
                                                <label class="col-md-2">故障信息</label>
                                                <div class="col-md-10">
                                                <input class="form-control " name="bug_info" value="{{$data->bug_info}}" /> 
                                            </div>
                                            </div>
                                            <div class="form-group col-md-8">
                                          <label class="col-md-2">备注</label>
                                          <div class="col-md-10">
                                                <input class="form-control " name="remark" value="{{$data->remark}}" />
                                          </div>       
                                            </div>

                                <div class="form-group text-center row">                                                
                                    <button type="submit" class="btn btn-success waves-effect waves-light">更新基本信息</button>
                                </div>
                                </form>
                                <hr/>

        <div class="row">
        <form name="statusForm" action="{{url('zadmin/task/change-status')}}">    
        <div class="form-group col-md-4">
            <label class="col-md-4">当前状态</label>
            <div class="col-md-8">
                <span class="btn btn-{{$data->status_style}}"> {{$data->status_txt}} </span>
            </div>
        </div>
        <div class="form-group col-md-4">
            <label class="col-md-4">修改状态</label>
            <div class="col-md-8">
                <select class="form-control" name="status">
                    @foreach($status_arr as $k=>$s)
                    <option value="{{$k}}" {{$k==75?'selected':''}}>{{$s}}</option>
                    @endforeach
                </select> 
            </div>
        </div>
        <div class="form-group col-md-4">
            
            <button class="btn btn-primary">修改状态</button>
        </div>
        <input type="hidden" name="id" value="{{$data->id}}">
        </form>
        </div>                        
                                            

    </div>
                                    </div>
                                </div>
                            </div>

                             <div class="col-lg-12">
                                <div class="panel panel-border panel-success">
                                    <div class="panel-heading">
                                        <h3 class="panel-title">项目信息</h3>
                                    </div>
                                    <div class="panel-body">
<div class="row">
    <table class="table table-hover" id="service">
        <thead>
            <tr>
                <th>项目类别</th>
                <th>项目名称</th> 
                <th>原价</th> 
                <th>折扣率</th>             
                <th>费用</th>
                <th width="200">操作</th>
            </tr>
        </thead>
        <tbody class="serviceitem">
        @foreach($data->service as $s)
            <tr>
                <td>{{object_get($s,'service_type')}}</td>
                <td>{{$s->service_name}}</td>
                <td>{{$s->source_price}}</td>
                <td>{{$s->discount}}</td>
                <td>{{$s->service_price}}</td>
                <td>
                    <!-- <button type="button" class="btn btn-sm btn-info btn-add">修改</button> -->
                  <a href="{{url('zadmin/task/del-service')}}?id={{$s->id}}&task_id={{$data->id}}" class="btn btn-sm btn-danger btn-remove-service">删除</a>
                </td>
            </tr>
        
        @endforeach        
        <tr>
            <form action="{{url('zadmin/task/save-service')}}" method="post">
            <td>
               
                <input class="form-control " name="service_type" />
            </td>
            <td>
                <input class="form-control " name="service_name" />
            </td>
            <td>
               
                <input class="form-control source_price" name="source_price" />
            </td>
            <td>
                <input class="form-control service_discount" name="discount" />
            </td>
            <td>
                <div class="input-group">
                        <input name="service_price" class="form-control service_price" />
                        <span class="input-group-btn">
                        <button type="button" class="btn waves-effect waves-light btn-primary">元</button>
                        </span>
                </div>
                                            
                </td>
                <td width="100">
                
                  <button class="btn btn-sm btn-success">保存</button>
                </td>
                <input name="task_id" value="{{$data->id}}" type="hidden" />
                {{csrf_field()}}
                </form>    
            </tr>

                                                </tbody>
                                            </table>
                                        </div>
                                    </div>
                                </div>
                            </div>

                             <div class="col-lg-12">
                                <div class="panel panel-border panel-primary">
                                    <div class="panel-heading">
                                        <h3 class="panel-title">配件信息</h3>
                                    </div>
                                    <div class="panel-body">
                                        <div class="row">
                                            <table class="table table-hover" id="depart">
                                                <thead>
                                                    <tr>
                                                        
                                                        <th>名称</th>
                                                        <th>数量</th>
                                                        <th>单价</th>
                                                        <th>折扣率</th>
                                                        <th>费用</th>
                                                        <th>操作</th>
                                                    </tr>
                                                </thead>
                            <tbody class="departitem">
                                @foreach($data->depart as $d)
                                <tr>
                                   
                                    <td>{{object_get($d,'depart_name')}}</td>
                                    <td>{{$d->depart_num}}</td>
                                    <td>{{$d->depart_price}}</td>
                                    <td>{{$d->discount}}</td>
                                    <td>{{$d->total_price}}</td>
                                    <td>
                                        <a href="{{url('zadmin/task/del-depart')}}?id={{$d->id}}&task_id={{$data->id}}" class="btn btn-danger btn-sm">删除</a></td>
                                </tr>
                                @endforeach
                                <tr>
                                <form action="{{url('zadmin/task/save-depart')}}" method="post">    
                                       
                                        <td>
                                             <input name="depart_name" class="form-control" />
                                        </td>
                                        <td>
                                            <input type="text" class="form-control depart_num" name="depart_num" value="1">
                                        </td>
                                        <td>
                                            <input type="text" class="form-control depart_price" name="depart_price" >
                                        </td>
                                        <td>
                                            <input type="text" class="form-control discount" name="discount" >
                                        </td>
                                        <td>
                                            <div class="input-group">
        <input name="total_price" class="form-control total_price" />
                <span class="input-group-btn">
        <button type="button" class="btn waves-effect waves-light btn-primary">元</button>
        </span>
                                                    </div>
                                            
                                        </td>
                <td width="100">
                    <button  class="btn btn-sm btn-success">保存</button>                  
                </td>
                <input name="task_id" value="{{$data->id}}" type="hidden" />
                {{csrf_field()}}
            </form>
            </tr>
                                                </tbody>
                                            </table>
                                        </div>
                                    </div>
                                </div>
                            </div>

                             

                           
                                
                                                    
                        </div>




                                

                                        

                                    </div>

                                </div>


                            </div>


                            
                        
                       
                                </div>
			                        	
			                        </div>

                             
			                        
                        			
                        		</div>
                                
                            </div> <!-- end col -->

                            
                        </div>
@endsection
                        
                        
                        

                  
            
    @section('modal')        
      
    @endsection        
       

           



    
        @section('js')
       

<script type="text/javascript" src="{{asset('admin/plugins/bootstrap-datetimepicker/js/bootstrap-datetimepicker.min.js')}}"></script>
<script type="text/javascript" src="{{asset('admin/plugins/bootstrap-datetimepicker/js/locales/bootstrap-datetimepicker.zh-CN.js')}}" charset="UTF-8"></script>
<script type="text/javascript">
$(document).ready(function () {  
    $(".serviceitem tr").find("select").select2();
    $('.departitem tr').find("select").select2();

   


    
});    
   

    
   

    

    $('.date').datetimepicker({
        format: "yyyy-mm-dd",
        autoclose: true,
        todayBtn: true,
        startDate: new Date()
       
    });

    $('#client_id').on('change',function(){
        var client_id = $(this).val();
        var url = "{{url('api/cars')}}";
       
        $.get(url,{'client_id':client_id},function(rs){
            if(rs.status==true) {
                var opt = '';
                $.each(rs.data,function(k,v){
                    opt = opt+'<option value="'+v.id+'">'+v.car_num+'</option>'
                });
                $('#car_id').append(opt);
            }
        })
    });

    $(document).on('change','.service_type',function(){
        var _this = $(this);
        var type_id = _this.val();
        var url = "{{url('api/services')}}";
        $.get(url,{'type_id':type_id},function(rs){
            if(rs.status==true) {
                var service = _this.parents('tr').find('.service_id');
                service.empty();
                var opt = '<option value="">请选择</option>';
                $.each(rs.data,function(k,v){
                    opt += '<option value="'+v.id+'" data-price="'+v.price+'">'+v.name+'</option>'; 
                });
                 service.append(opt);
                
            }
        })
    });

    $(document).on('change','.service_id',function(){
        var _this = $(this);
        //var price = _this.find("option:selected").data('price')
        //alert(price);
        //_this.parents('tr').find('.service_price').val(price);
        
        
    });

    $(document).on('change','.depart_cate',function(){
        var _this = $(this);
        var cate_id = _this.val();
        var url = "{{url('api/departs')}}";
        $.get(url,{'cate_id':cate_id},function(rs){
            if(rs.status==true) {
                var depart = _this.parents('tr').find('.depart_id');
                depart.empty();
                var opt = '<option value="">请选择</option>';
                $.each(rs.data,function(k,v){
                    opt += '<option value="'+v.id+'" data-price="'+v.out_price+'" data-num="'+v.store_num+'">'+v.name+'</option>'; 
                });
                 depart.append(opt);
                
            }
        })
    });

   

    $(document).on('click','.total_price',function(){
        var _this = $(this);
        var num   = _this.parents('tr').find('.depart_num').val();
        var price    =  _this.parents('tr').find('.depart_price').val();
        var discount = _this.parents('tr').find('.discount').val();       
        
        _this.val(price*num*discount);
    });


    $(document).on('click','.service_price',function(){
        var _this = $(this);
       
        var price    =  _this.parents('tr').find('.source_price').val();
        var discount = _this.parents('tr').find('.service_discount').val();       
        
        var discount_price = (price*discount).toFixed(2);
       _this.val(discount_price);
    });

    




         

         
        </script>
@endsection
    
